package store.lunangangster.bpmn.converter.parser;


import javax.xml.stream.XMLStreamReader;
import org.apache.commons.lang3.StringUtils;
import store.lunangangster.bpmn.constant.BpmnXmlConstant;
import store.lunangangster.bpmn.model.BpmnModel;
import store.lunangangster.bpmn.model.Process;

/**
 * 用于{@link Process}对象的解析器
 *
 * @author cwk
 * @version 1.0
 */
public class ProcessParser implements BpmnXmlConstant {

  public Process parse(XMLStreamReader xsr, BpmnModel bpmnModel) throws Exception {
    Process process = null;

    String processId = xsr.getAttributeValue(null, ATTRIBUTE_COMMON_ID);
    if (StringUtils.isNotBlank(processId)) {
      process = new Process();
      process.setId(processId);
      process.setName(xsr.getAttributeValue(null, ATTRIBUTE_COMMON_NAME));
      String processIsExecutable = xsr.getAttributeValue(null,
          ATTRIBUTE_PROCESS_EXECUTABLE);
      if (StringUtils.isNotBlank(processIsExecutable)) {
        process.setExecutable(Boolean.parseBoolean(processIsExecutable));
      }
      bpmnModel.getProcesses().add(process);
      bpmnModel.addToElementMap(process);
    }

    return process;
  }
}
